package org.homework;

public class SupermarketService {
    private Supermarket supermarket = new Supermarket();

    /**
     * 增加商品
     * @param product
     */
    public void addProduct(Product product) {
        supermarket.getProducts().add(product);
    }

    /**
     * 删除商品
     * @param product
     */
    public void removeProduct(Product product) {
        supermarket.getProducts().remove(product);
    }

    /**
     * 更新商品
     * @param product
     */
    public void updateProduct(Product product) {
        supermarket.getProducts().remove(product);
        supermarket.getProducts().add(product);
    }
    /**
     * 购买商品，根据商品名和购买数量减少库存
     */
    public String purchaseProduct(String productName, int quantity) {
        for (Product product : supermarket.inventory) {
            if (product.getName().equals(productName)) {
                // 判断库存是否充足
                if (product.getQuantity() >= quantity) {
                    product.setQuantity(product.getQuantity() - quantity);
                    return "成功购买 " + quantity + " 件 " + productName;
                } else {
                    return "抱歉，" + productName + " 库存不足，无法购买 " + quantity + " 件。";
                }
            }
        }
        return "找不到名为 " + productName + " 的商品。";
    }

    /**
     * 显示超市当前库存的方法
     */
    public void displayInventory() {
        System.out.println("超市当前库存情况：");
        for (Product item : supermarket.inventory) {
            System.out.println(item.getName() + ": " + item.getQuantity() + "件");
        }
    }

    public void view(){
        System.out.println("=====欢迎来到爱心超市=====");
        System.out.println("1. 购买商品");
        System.out.println("2. 查看库存");
        System.out.println("3. 退出系统");
        System.out.println("=====欢迎来到爱心超市=====");
    }
    public void purchase() {
        Product apple = new Product("苹果", 5.0, 10, "水果", "很甜很好吃！");
        Product banana = new Product("香蕉", 3.0, 20, "水果", "很甜很好吃！");
        Product milk = new Product("牛奶", 8.0, 5, "饮品", "好喝有营养！");
        supermarket.inventory.add(apple);
        supermarket.inventory.add(banana);
        supermarket.inventory.add(milk);
    }
}
